// --------------------------------------------------------------------------------------------------------------------
// <copyright file="Program.cs" company="">
//   
// </copyright>
// <summary>
//   Defines the Program type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

using DbDiff.Common.Comparer;
using DbDiff.Common.Model;

namespace DbdiffCmd
{
    using System;
    using System.Data;
    using System.Text;
    using DbDiff.Common.Helper;
    using DbDiff.Common.Scripting;

    /// <summary>
    /// The program.
    /// </summary>
    public static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        /// <param name="args">
        /// The argsuments
        /// </param>
        [STAThread]
        public static void Main(string[] args)
        {
            string userName = string.Empty;
            string password = string.Empty;
            string outputDirectory;
            bool sqlAuth = false;

            string serverName = args[0];
            string databaseName = args[1];
            if (args.Length > 3)
            {
                userName = args[2];
                password = args[3];
                sqlAuth = true;
                outputDirectory = args[4];
            }
            else
            {
                outputDirectory = args[2];
            }


            var scripter = new Scripter(serverName, databaseName, userName, password, sqlAuth, outputDirectory);
            scripter.StoredProcedures();
            scripter.Tables(null);
        }

        /// <summary>
        /// The scripter.
        /// </summary>
        public class Scripter
        {
            /// <summary>
            /// The server.
            /// </summary>
            private readonly RegisteredServer server;

            /// <summary>
            /// The db info.
            /// </summary>
            private readonly DatabaseInfo databaseInfo;

            /// <summary>
            /// The out directory.
            /// </summary>
            private readonly string outDirectory;

            /// <summary>
            /// Initializes a new instance of the <see cref="Scripter"/> class.
            /// </summary>
            /// <param name="serverName">
            /// The server name.
            /// </param>
            /// <param name="databaseName">
            /// The database name.
            /// </param>
            /// <param name="userName">
            /// The user name.
            /// </param>
            /// <param name="password">
            /// The password.
            /// </param>
            /// <param name="sqlAuth">
            /// The is sql auth.
            /// </param>
            /// <param name="outDirectory">
            /// The out directory.
            /// </param>
            public Scripter(
                            string serverName, 
                                          string databaseName, 
                                          string userName, 
                                          string password, 
                                            bool sqlAuth, 
                                            string outDirectory)
            {
                AuthType at = sqlAuth ? AuthType.Sql : AuthType.Win;

                this.server = new RegisteredServer(serverName, at, userName, password);

                this.databaseInfo = new DatabaseInfo();
                this.databaseInfo.Connect(this.server, databaseName);

                this.databaseInfo.SqlObjectOptions.CompareOptionsList.SetValueByName(OptionNames.CompareDatabase, "false");
                this.databaseInfo.SqlObjectOptions.CompareOptionsList.SetValueByName(OptionNames.CompareProc, "false");
                this.databaseInfo.SqlObjectOptions.CompareOptionsList.SetValueByName(OptionNames.CompareFunction, "false");
                this.databaseInfo.SqlObjectOptions.CompareOptionsList.SetValueByName(OptionNames.CompareTrigger, "false");
                this.databaseInfo.SqlObjectOptions.CompareOptionsList.SetValueByName(OptionNames.CompareView, "false");
                this.databaseInfo.SqlObjectOptions.CompareOptionsList.SetValueByName(OptionNames.CompareServer, "false");
                this.databaseInfo.SqlObjectOptions.CompareOptionsList.SetValueByName(OptionNames.CompareUdt, "false");
                this.databaseInfo.SqlObjectOptions.CompareOptionsList.SetValueByName(OptionNames.CompareSchema, "false");

                int i = this.databaseInfo.SqlObjectOptions.CompareOptionsList.FindIndex(z => z.Name == OptionNames.Name);

                if (i > -1)
                {
                    this.databaseInfo.SqlObjectOptions.CompareOptionsList.SetValueByName(OptionNames.Name, "Address");
                }
                
                this.databaseInfo.FillDatabaseInfo(false, SelectedDatabase.Left);
                this.outDirectory = outDirectory;
            }

            /// <summary>
            /// The stored procedures.
            /// </summary>
            public void StoredProcedures()
            {
                var scriptingOption = new SqlScriptingOption { Alter = false };
                //var script = new SqlScripting(this.databaseInfo.DatabaseObjects, scriptingOption);
                /*
                foreach (DataRow dr in this.databaseInfo.DatabaseObjects.DatabaseInfo.DatabaseDataSets.tblProcedure.Rows)
                {
                    string fileName = string.Format(@"{0}\{1}.{2}.sql", this.outDirectory, dr["SchemaName"], dr["Name"]);

                    string s = script.Script(dr["SchemaName"].ToString(), dr["Name"].ToString(), DatabaseObjectTypes.Procedure);

                    if (System.IO.File.Exists(fileName))
                    {
                        System.IO.File.Delete(fileName);
                    }

                    System.IO.File.WriteAllText(fileName, s, Encoding.Unicode);
                }
                 * */
            }

            /// <summary>
            /// The tables.
            /// </summary>
            /// <param name="tableName">
            /// The table name.
            /// </param>
            public void Tables(string tableName)
            {
                var scriptingOption = new SqlScriptingOption { Alter = false };

                //var script = new SqlScripting(this.databaseInfo, scriptingOption);

                /*foreach (DataRow dr in this.databaseInfo.DatabaseObjects.DatabaseInfo.DatabaseDataSets.tblTable.Rows)
                {
                    string fileName = string.Format(@"{0}\{1}.{2}.sql", this.outDirectory, dr["SchemaName"], dr["TableName"]);
                    string s = script.ScriptTable(dr["SchemaName"].ToString(), dr["TableName"].ToString());

                    if (System.IO.File.Exists(fileName))
                    {
                        System.IO.File.Delete(fileName);
                    }

                    System.IO.File.WriteAllText(fileName, s, Encoding.Unicode);
                }
                 * */
            }
        }
    }
}